package com.sch.wanandroid.base

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.sch.wanandroid.ui.login.LoginActivity
import com.sch.wanandroid.util.AppManager

/**
 * Created by Sch.
 * Date: 2020/12/14
 * description:
 */
abstract class BaseFragment : Fragment() {
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        init(savedInstanceState)
    }

    protected abstract fun init(savedInstanceState: Bundle?)

    /**
     * 界面跳转
     * @param isLogin 启动界面是否需要登录
     */
    protected fun intent(clazz: Class<*>, isLogin: Boolean) {
        //需要登录&&未登录
        if (isLogin && !AppManager.isLogin()) {
            startActivity(Intent(requireActivity(), LoginActivity::class.java))
        } else {
            startActivity(Intent(requireActivity(), clazz))
        }
    }

    /**
     * 携带bundle跳转
     * @param isLogin 启动界面是否需要登录
     */
    protected fun intent(bundle: Bundle, clazz: Class<*>, isLogin: Boolean) {
        //需要登录&&未登录
        if (isLogin && !AppManager.isLogin()) {
            startActivity(Intent(requireActivity(), LoginActivity::class.java))
        } else {
            startActivity(Intent(requireActivity(), clazz).apply {
                putExtras(bundle)
            })
        }
    }
}